package com.fingard.dsp.bank.directbank.hebbank01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;


public class HEBBANK01SinglePay extends HEBBANK01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        // 返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        retAts.writeStart();
        if ("1".equals(p_req1908.singleDetail.sameBnk)) {    //同行转账
            sameBankPay(retAts, p_req1908);
        } else if ("0".equals(p_req1908.singleDetail.sameBnk)) { //行外转账
            crossBankPay(retAts, p_req1908);
        } else {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo("同行标志为空");
        }
        return retAts;
    }

    /**
     * 跨行转账
     *
     * @param retAts
     * @param p_req1908
     * @Author 范天星/2020-03-10
     */
    private void crossBankPay(RetAtsSingle retAts, ReqSingle p_req1908) throws Exception {
        try {
            HEBBANK01Header bankHeader = new HEBBANK01Header();
            bankHeader.bsnCode = "CBE008";
            bankHeader.serialNo = p_req1908.singleDetail.askNumber;
            //新建发送银行报文
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeElementString("areaCode", "0000");
            xmlWriter.writeStartElement("ReqParam");
            xmlWriter.writeElementString("Group_Company_name", p_req1908.singleDetail.payName);
            xmlWriter.writeElementString("Group_Company_Acc", p_req1908.singleDetail.payAct);
            xmlWriter.writeElementString("DAXIBZ", "");
            xmlWriter.writeElementString("Urgent_Flag", "1".equals(p_req1908.singleDetail.paySpd) ? "1" : "0");
            xmlWriter.writeElementString("Pay_Sys_No", p_req1908.singleDetail.recBankCode);
            xmlWriter.writeElementString("Bank_Name", p_req1908.singleDetail.recBankName);
            xmlWriter.writeElementString("Rec_Dep_name", p_req1908.singleDetail.recName);
            xmlWriter.writeElementString("Rec_Dep_name", p_req1908.singleDetail.recName);
            xmlWriter.writeElementString("PayAmt", p_req1908.singleDetail.payAmount);
            xmlWriter.writeElementString("Account_cur", convertCurToBank(p_req1908.singleDetail.payCur));
            xmlWriter.writeElementString("Pay_Use", p_req1908.singleDetail.purpose + "＋" +
                    p_req1908.singleDetail.abstractATS);
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] bankResult = sendToBank(xmlWriter.toXmlString(), bankHeader);
            if (bankResult[0].length() == 0) {
                String[] result = bankResult[1].split("\\|");
                if ("0000".equals(result[0])) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    String xmlStr = bankResult[1].substring(bankResult[1].indexOf("|#") + 2);
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(xmlStr, tmpSbMsg);
                    if (tmpBkReader != null) {
                        RetAtsSglDetail retDetail = new RetAtsSglDetail();
                        retDetail.respCode = "0";
                        retDetail.respInfo = "";
                        retDetail.transState = tmpBkReader.getFirstText("/SCCBEBankData/opRep/opResult/YDDM")
                                + tmpBkReader.getFirstText("/SCCBEBankData/opRep/opResult/TranStt");
                        retDetail.payInfoCode = retDetail.transState;
                        retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                        retAts.writeRetDetail(retDetail);
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                    }
                } else {
                    WriteBankLogLn("支付提交失败，失败原因：" + result[1]);
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(result[1]);
                }
            } else {
                WriteBankLogLn("支付报文提交失败，失败原因：" + bankResult[1]);
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(bankResult[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            WriteBankLogLn("发生错误：" + e.getMessage());
        }
    }

    /**
     * 同行转账
     *
     * @param retAts
     * @param p_req1908
     * @Author 范天星/2020-03-11
     */
    private void sameBankPay(RetAtsSingle retAts, ReqSingle p_req1908) throws Exception {
        try {
            HEBBANK01Header bankHeader = new HEBBANK01Header();
            bankHeader.bsnCode = "CBE007";
            bankHeader.serialNo = p_req1908.singleDetail.askNumber;
            //新建发送银行报文
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeElementString("areaCode", "0000");
            xmlWriter.writeStartElement("ReqParam");
            xmlWriter.writeElementString("Group_Company_name", p_req1908.singleDetail.payName);
            xmlWriter.writeElementString("Group_Company_Acc", p_req1908.singleDetail.payAct);
            xmlWriter.writeElementString("Rec_Dep_name", p_req1908.singleDetail.recName);
            xmlWriter.writeElementString("Rec_Dep_name", p_req1908.singleDetail.recName);
            xmlWriter.writeElementString("PayAmt", p_req1908.singleDetail.payAmount);
            xmlWriter.writeElementString("Account_cur", convertCurToBank(p_req1908.singleDetail.payCur));
            xmlWriter.writeElementString("Pay_Use", p_req1908.singleDetail.purpose);
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] bankResult = sendToBank(xmlWriter.toXmlString(), bankHeader);
            if (bankResult[0].length() == 0) {
                String[] result = bankResult[1].split("\\|");
                if ("0000".equals(result[0])) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    String xmlStr = bankResult[1].substring(bankResult[1].indexOf("|#") + 2);
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(xmlStr, tmpSbMsg);
                    if (tmpBkReader != null) {
                        RetAtsSglDetail retDetail = new RetAtsSglDetail();
                        retDetail.respCode = "0";
                        retDetail.respInfo = "";
                        retDetail.transState = tmpBkReader.getFirstText("/SCCBEBankData/opRep/opResult/YDDM")
                                + tmpBkReader.getFirstText("/SCCBEBankData/opRep/opResult/TranStt");
                        retDetail.payInfoCode = retDetail.transState;
                        retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                        retAts.writeRetDetail(retDetail);
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                    }
                } else {
                    WriteBankLogLn("支付提交失败，失败原因：" + result[1]);
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(result[1]);
                }
            } else {
                WriteBankLogLn("支付报文提交失败，失败原因：" + bankResult[1]);
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(bankResult[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            WriteBankLogLn("发生错误：" + e.getMessage());
        }
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail();

        try {
            HEBBANK01Header bankHeader = new HEBBANK01Header();
            bankHeader.bsnCode = "CBE013";
            bankHeader.serialNo = p_req1944.reqHeader.reqSeqID;
            //新建发送银行报文
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeElementString("areaCode", "0000");
            xmlWriter.writeStartElement("ReqParam");
            xmlWriter.writeElementString("Acctno", p_req1944.payAct);
            xmlWriter.writeElementString("TranSeqNo", p_req1944.askNumber);
            xmlWriter.writeElementString("TranTime",
                    Format.strToDateFormat("yyyy-MM-dd HH:mm:ss",
                            "yyyyMMddHHmmss", p_req1944.bgnDate));
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] bankResult = sendToBank(xmlWriter.toXmlString(), bankHeader);
            if (bankResult[0].length() == 0) {
                String[] result = bankResult[1].split("\\|");
                if ("0000".equals(result[0])) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    String xmlStr = bankResult[1].substring(bankResult[1].indexOf("|#") + 2);
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(xmlStr, tmpSbMsg);
                    if (tmpBkReader != null) {
                        retDetail.respCode = "0";
                        retDetail.respInfo = tmpBkReader.getFirstText("/SCCBEBankData/opRep/opResult/SBYY");
                        retDetail.reqSeqID = p_req1944.askNumber;
                        retDetail.transState = tmpBkReader.getFirstText("/SCCBEBankData/opRep/opResult/Proc_stat");
                        retDetail.payInfoCode = retDetail.transState;
                        retDetail.transAmt = tmpBkReader.getFirstText("/SCCBEBankData/opRep/opResult/PayAmt");
                    } else {
                        WriteBankLogLn("查询提交失败，失败原因：" + result[1]);
                        retDetail.respCode = "0";
                        retDetail.respInfo = result[1];
                    }
                } else {
                    WriteBankLogLn("查询提交失败，失败原因：" + result[1]);
                    retDetail.respCode = "0";
                    retDetail.respInfo = result[1];
                }
            } else {
                WriteBankLogLn("查询报文提交失败，失败原因：" + bankResult[1]);
                retDetail.respCode = "0";
                retDetail.respInfo = bankResult[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "0";
            retDetail.respInfo = e.getMessage();
            WriteBankLogLn("查询发生异常，异常信息为：" + e.getMessage());
        }
        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
